[BITS 16]
[CPU 286]

global __gestion_touches

%ifndef BCC
[SEGMENT _TEXT class=CODE]
%else
[SECTION .text]
%endif



; following values are an offset into the stack
delay  equ   10
dim_x  equ   8
dim_x8 equ   6
dim_y  equ   4
pos_x  equ   2
pos_y  equ   0

__gestion_touches:
  .touches_loop:
    mov ax,80h    ; Test touche ESC
    out 13h,ax
    in al,13h
    mov bl,al
    mov ax,0
    out 13h,ax

    mov ax,100h   ; Test touche shift
    out 13h,ax
    in al,13h
    mov bh,al
    mov ax,0
    out 13h,ax

    mov ax,200h   ; Lire ligne 9 : direction
    out 13h,ax
    in  al,13h
    mov cl,al
    mov ax,0
    out 13h,ax
    mov al,cl

    .bas:
    ;Test bas
    test1 al,4
    jz .finbas
      cmp word [ss:bp+pos_y],0
      je .finbas
      dec word [ss:bp+pos_y]
      test1 bh,6
      jz .finbas
      cmp word [ss:bp+pos_y],0
      je .finbas
      dec word [ss:bp+pos_y]
    .finbas:

    .haut:
    ;Test haut
    test1 al,5
    jz .finhaut
      mov cx,[ss:bp+dim_y]
      sub cx,64
      cmp cx,[ss:bp+pos_y]
      je .finhaut
      inc word [ss:bp+pos_y]
      test1 bh,6
      jz .finhaut
      cmp cx,[ss:bp+pos_y]
      je .finhaut
      inc word [ss:bp+pos_y]
    .finhaut:

    .droite:
    ;Test droite
    test1 al,3
    jz .findroite
      cmp word [ss:bp+pos_x],0
      je .findroite
      dec word [ss:bp+pos_x]
      test1 bh,6
      jz .findroite
      cmp word [ss:bp+pos_x],0
      je .findroite
      dec word [ss:bp+pos_x]
    .findroite:

    .gauche:
    ;Test gauche
    test1 al,6
    jz .fingauche
      mov cx,[ss:bp+dim_x]
      sub cx,128
      cmp cx,[ss:bp+pos_x]
      je .fingauche
      inc word [ss:bp+pos_x]
      test1 bh,6
      jz .fingauche
      cmp cx,[ss:bp+pos_x]
      je .fingauche
      inc word [ss:bp+pos_x]
    .fingauche:

    .esc:
    test1 bl,3
    jz .finesc
; return and stop
    mov al,1
ret
    .finesc:

    .shift:
    ;Test shift
    test1 bh,6
    jnz .nodelay
      ;Faire une pause
      mov cx,[ss:bp+delay]
      .delay_lp:
        nop
        nop
        nop
      loop .delay_lp
    .nodelay:
    ;Si on a pressé aucune touche on continue de tourner
    cmp al,0
    jne .touchepressee
    hlt
  jmp .touches_loop
  .touchepressee:
; return and continue
  mov al,0
ret